// SPDX-License-Identifier: GPL-3.0-or-later

import QtQuick
import QtQuick.Controls
import Fk

Item {
  id: root
  width: 20
  height: 20
  scale: 1
  property string skillname: "zhiheng"
  property string skilltype: "limit" // limit, wake, ...
  property int usedtimes: -1 // -1 will not be shown
  property real markScale: 1

  property bool isRotating: false
  property real flipAngle: 0
  property int flipCount: 0
  property int targetFlips: 5

  property string yangSource: Cpp.path + "/packages/lunarltk-qsgs-ui/image/mark/@switch"
  property string yinSource: Cpp.path + "/packages/lunarltk-qsgs-ui/image/mark/@switch-yin"

  property int lastFlipCount: -1

  Item {
    id: imageContainer
    anchors.fill: parent
    
    Image {
      id: yangImage
      anchors.fill: parent
      source: root.yangSource
      width: sourceSize.width * markScale
      height: sourceSize.height * markScale
      visible: true
      
      transform: Rotation {
        axis.x: 0
        axis.y: 1
        axis.z: 0
        angle: root.flipAngle <= 90 ? root.flipAngle : 180 - root.flipAngle
        origin.x: yangImage.width / 2
        origin.y: yangImage.height / 2
      }
    }
    
    Image {
      id: yinImage
      anchors.fill: parent
      source: root.yinSource
      width: sourceSize.width * markScale
      height: sourceSize.height * markScale
      visible: root.flipAngle > 90 && root.flipAngle <= 270
      
      transform: Rotation {
          axis.x: 0
          axis.y: 1
          axis.z: 0
          angle: root.flipAngle > 90 ? root.flipAngle - 180 : root.flipAngle
          origin.x: yinImage.width / 2
          origin.y: yinImage.height / 2
      }
    }
  }

  Text {
    id: name
    anchors.centerIn: imageContainer
    color: "#d6d6d6"
    font.pixelSize: 15
    font.family: Config.li2Name
    style: Text.Outline
    styleColor: "#3D2D1C"
    text: Lua.tr(skillname);
    visible: false
  }

  MouseArea {
    anchors.fill: parent
    hoverEnabled: root.visible
    
    ToolTip {
      id: markDetail
      x: 20
      y: Math.round((parent.height - height) / 2)
      height: Math.min(contentHeight + 20, 100)
      visible: false
      delay: 0
      timeout: 6000

      contentItem: Text{
        id: markDetailText
        text: "<b>" + Lua.tr(skillname) + "</b><br>" + (usedtimes >= 1? "已发动":"未发动")
        font.pixelSize: 15
        wrapMode: Text.WordWrap
        textFormat: TextEdit.RichText
        color: "#fff7e7"
        font.family: "SimSun"
        style: Text.Outline
        styleColor: "#000000"
      }

      background: Rectangle { // same as cheatDrawer
        color: "#CC2E2C27"
        radius: 5
        border.color: "#A6967A"
        border.width: 1
      }
    }
    
    onEntered: {
      markDetail.visible = true
    }
    onExited: {
      markDetail.visible = false
    }
  }

  PropertyAnimation {
    id: flipAnimation
    target: root
    property: "flipAngle"
    from: 0
    to: 180 * root.targetFlips // 总旋转角度 = 180° × 翻转次数
    duration: 1200 // 总动画时间1.2秒
    easing.type: Easing.InOutQuad
    running: root.isRotating
    
    onStarted: {
      root.flipCount = 0;
      root.lastFlipCount = -1;
      
      root.flipAngle = 0;
    }
    
    onStopped: {
      root.isRotating = false;
      
      if (skilltype === 'switch') {
        if (usedtimes < 1) {
          // 显示阳
          yangImage.visible = true;
          yinImage.visible = false;
        } else {
          // 显示阴
          yangImage.visible = false;
          yinImage.visible = true;
        }
        root.flipAngle = 0;
      }
    }
  }

  onFlipAngleChanged: {
    if (!isRotating) return;
    
    var currentFlip = Math.floor(flipAngle / 180);
    var angleInFlip = flipAngle % 180;

    if (currentFlip == 1) scale = 1.4;
    if (currentFlip == 3) scale = 1;
    
    if (angleInFlip >= 85 && angleInFlip <= 95) {
      // 确保只在每次翻转的90°附近切换一次
      if (currentFlip > root.lastFlipCount) {
        root.lastFlipCount = currentFlip;
        
        if (yangImage.visible) {
          yangImage.visible = false;
          yinImage.visible = true;
        } else {
          yangImage.visible = true;
          yinImage.visible = false;
        }
      }
    }
  }

  function startFlipAnimation() {
    if (skilltype !== 'switch') return;
    if (usedtimes < 1) {
      if (yangImage.visible && !yinImage.visible) return;
    } else {
      if (!yangImage.visible && yinImage.visible) return;
    }
    
    root.flipAngle = 0;
    root.lastFlipCount = -1;
    root.isRotating = true;
    
    flipAnimation.start();
  }

  onSkillnameChanged: {
    let data = Lua.call("GetSkillData", skillname);
    if (data.frequency || data.switchSkillName) {
      skilltype = data.switchSkillName ? 'switch' : data.frequency;
      visible = true;
    } else {
      visible = false;
    }
  }

  onUsedtimesChanged: {
    name.visible = false;
    visible = false;
    if (usedtimes > -1) {
      visible = true;
    }
    
    if (skilltype === "wake") {
      visible = (usedtimes > 0);
      yangImage.source = Cpp.path + "/packages/lunarltk-qsgs-ui/image/mark/@wake";
      yangImage.visible = true;
      yinImage.visible = false;
      markDetailText.text = "<b>" + Lua.tr(skillname) + "</b><br>已觉醒"
    } else if (skilltype === "limit") {
      if (usedtimes >= 1) {
        yangImage.source = Cpp.path + "/packages/lunarltk-qsgs-ui/image/mark/@limit-used";
      } else {
        yangImage.source = Cpp.path + "/packages/lunarltk-qsgs-ui/image/mark/@limit";
      }
      yangImage.visible = true;
      yinImage.visible = false;
    } else if (skilltype === 'switch') {
      visible = true;
      
      yangImage.source = root.yangSource;
      yinImage.source = root.yinSource;
      
      startFlipAnimation();
      
      markDetailText.text = "<b>" + Lua.tr(skillname) + "</b>·"+(usedtimes < 1 ? '阳' : '阴')
    } else if (skilltype === 'quest') {
      if (usedtimes > 1) {
        name.visible = true;
        yangImage.source = Cpp.path + "/packages/lunarltk-qsgs-ui/image/mark/@quest-used";
      } else {
        name.visible = root.visible
        name.color = "#e0e0e0"
        name.styleColor = "#93211d"
        yangImage.source = Cpp.path + "/packages/lunarltk-qsgs-ui/image/mark/@quest";
      }
      yangImage.visible = true;
      yinImage.visible = false;
      markDetailText.text = "<b>" + Lua.tr(skillname) + (usedtimes > 1 ? "失败" : "成功") + "</b>"
    } else {
      yangImage.source = Cpp.path + "/packages/lunarltk-qsgs-ui/image/mark/@" + skilltype;
      yangImage.visible = true;
      yinImage.visible = false;
    }
    
    var regex = /[\u{4E00}-\u{9FFF}\u{3400}-\u{4DBF}\u{20000}-\u{2A6DF}\u{2A700}-\u{2B73F}\u{2B740}-\u{2B81F}\u{2B820}-\u{2CEAF}\u{2CEB0}-\u{2EBEF}]/u;
    var match = Lua.tr(skillname).match(regex);
    name.text = match ? match[0] : "";
  }
}